package com.guiwu.gteway.industryapp.Api;

import android.app.Application;

import com.guiwu.gteway.industryapp.Api.http.HttpExecutor;
import com.guiwu.gteway.industryapp.Api.Urlmodule.UrlGet;
import com.guiwu.gteway.industryapp.Api.http.Urls;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;

import static com.guiwu.gteway.industryapp.Api.http.HttpExecutor.API_KEY;
import static com.guiwu.gteway.industryapp.Api.http.HttpExecutor.CAMERA_SERIAL;
import static com.guiwu.gteway.industryapp.Api.http.HttpExecutor.CONTENT_TYPE;
import static com.guiwu.gteway.industryapp.Api.http.HttpExecutor.GATEWAY_SN;
import static com.guiwu.gteway.industryapp.Api.http.HttpExecutor.USER_ID;


public class GuiwuApi {

    public static final String LOG_TAG = "GuiWuApi";
    private static HttpExecutor sHttpExecutor;
    static boolean sDebug;

    /**
     * 初始化
     * @param application
     */
    public static void init(Application application,boolean debug){

        sDebug = debug;
        OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient.Builder();
        if (sDebug) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLogger());
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            okhttpClientBuilder.addNetworkInterceptor(loggingInterceptor);
        }
    //    okhttpClientBuilder.addInterceptor(sApiInterceptor);

        sHttpExecutor = new HttpExecutor(okhttpClientBuilder.build());
    }

//    /**
//     * 通过拦截器添加请求头
//     */
//    private static Interceptor sApiInterceptor = new Interceptor() {
//
//        @Override
//        public Response intercept(Chain chain) throws IOException {
//            Request.Builder builder = chain.request().newBuilder();
//
//            return chain.proceed(builder.build());
//        }
//    };

    /**
     * HTTP GET method
     * @param url
     * @param callback
     */
    public static void get(String url, GuiwuApiCallback callback, Map<String ,String> headerMap) {
        sHttpExecutor.get(url, new GuiwuApiCallbackAdapter(callback),headerMap);
    }

    public static void get(String url, GuiwuApiCallback callback, String headType,String head) {
        sHttpExecutor.get(url, new GuiwuApiCallbackAdapter(callback),headType,head);
    }

//
    /**
     * HTTP POST method  默认请求头
     *
     * @param url
     * @param requestBodyString
     * @param callback
     */
    public static void post(String url, String requestBodyString, GuiwuApiCallback callback) {
        post(url,requestBodyString,callback,null,null);
    }


    public static void post(String url,String requestBodyString ,GuiwuApiCallback callback,String headType, String head){
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestBodyString);
        sHttpExecutor.post(url, requestBody, new GuiwuApiCallbackAdapter(callback),headType,head);
    }

    /**
     * HTTP PUT method
     *
     * @param url
     * @param requestBodyString
     * @param callback
     */
    public static void put(String url, String requestBodyString, GuiwuApiCallback callback) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestBodyString);
        sHttpExecutor.put(url, requestBody, new GuiwuApiCallbackAdapter(callback));
    }


    /****************************用户相关api*******************************/

    /**
     * 用户登录
     * @param requestBodyString 登录请求post
     * @param callback 回调接口对象
     */
    public static void userLogin(String requestBodyString,GuiwuApiCallback callback){
        post(UrlGet.urlForLogin(),requestBodyString,callback);
    }

    /**
     * 获取用户信息
     * @param userId
     * @param callback
     */
    public static void getUserInfo(String userId,GuiwuApiCallback callback){
        get(UrlGet.urlForUserInfo(),callback,USER_ID,userId);
    }


    /**
     * 更改密码
     * @param requestBodyString
     * @param callback
     */
    public static void changePassword(String requestBodyString,GuiwuApiCallback callback){
        put(UrlGet.urlForChangePassword(),requestBodyString,callback);
    }

    /**
     * 获取短信验证码
     * @param requestBodyString
     * @param callback
     */
    public static void getVertifyCode(String requestBodyString,GuiwuApiCallback callback){
        post(UrlGet.urlForVertifyCode(),requestBodyString,callback);
    }

    /**
     * 用户注册
     * @param requestBodyString
     * @param callback
     */
    public static void userRegister(String requestBodyString,GuiwuApiCallback callback){
        post(UrlGet.urlForRegiste(),requestBodyString,callback);
    }

    /**
     * 获取用户绑定萤石云的appKey
     */
    public static void getUserAppKey(String userId,GuiwuApiCallback callback){
        get(UrlGet.urlForAppKey(),callback,USER_ID,userId);
    }

    /**
     * 获取用户下挂网关
     * @param userId
     * @param callback
     */
    public static void getGateWayGroup(String userId, GuiwuApiCallback callback){
        get(UrlGet.urlForGetGateWayGroup(),callback,USER_ID,userId);
    }

    /**
     *  批量查询网关状态
     * @param apiKey
     * @param ids
     * @param callback
     */
    public static void queryGateWaySate(String apiKey, List<String> ids ,GuiwuApiCallback callback){
        get(UrlGet.urlForGateWaysState(ids),callback,API_KEY,apiKey);
    }

    public static void queryDimGateWayState(String apiKey ,GuiwuApiCallback callback){
        get(UrlGet.urlForDimGateWayState(),callback,API_KEY ,apiKey);
    }

    /**
     * 获取网关下的摄像头信息
     * @param gateWaysn
     * @param callback
     */
    public static void getGateWayVideoInfo(String gateWaysn,GuiwuApiCallback callback){
        get(UrlGet.urlForGetCameraInfo(),callback,GATEWAY_SN,gateWaysn);
    }

    /**
     * 获取摄像头acessToken
     * @param deviceSerial
     * @param callback
     */
    public static void getVideoAcessToken(String deviceSerial,GuiwuApiCallback callback){
        get(UrlGet.urlForAccesstoken(),callback,CAMERA_SERIAL,deviceSerial);
    }


    /**
     * 获取网关下监控点
     * @param gateWaySn
     * @param callback
     */
    public static void getMonitorPoints(String gateWaySn, String userId,GuiwuApiCallback callback){
        Map<String ,String> heads = new HashMap<>();
        heads.put(USER_ID,userId);
        heads.put(GATEWAY_SN,gateWaySn);
        get(UrlGet.urlForGetMonitorPoint(),callback,heads);
    }

    /**
     * 获取网关监控点的所有数据
     * @param gataWayId
     * @param apiKey
     * @param callback
     */
    public static void getMonitorData(String gataWayId,String apiKey,GuiwuApiCallback callback){
        get(UrlGet.urlForGetMonitorData(gataWayId),callback,API_KEY,apiKey);
    }

    /**
     * 控制监控点
     * @param gataWayId
     * @param apiKey
     * @param callback
     */
    public static void controlMonitor(String requestBody , String gataWayId ,String apiKey, GuiwuApiCallback callback){
        post(UrlGet.urlForControlMonitor(gataWayId),requestBody,callback,API_KEY,apiKey);
    }


    /**
     * 查询网关下事件表
     * @param gateWaysn
     * @param callback
     */
    public static void queryQueryEventList(String gateWaysn,GuiwuApiCallback callback){
        Map<String ,String> heads = new HashMap<>();
        heads.put(CONTENT_TYPE,"application/json");
        heads.put(GATEWAY_SN,gateWaysn);
        get(UrlGet.urlForQueryEventList(),callback,heads);
    }

    /**
     * 查询绑定的事件寄存器
     * @param gateWaysn
     * @param callback
     */
    public static void findRegistEvent(String gateWaysn,GuiwuApiCallback callback){
        Map<String ,String> heads = new HashMap<>();
        heads.put(CONTENT_TYPE,"application/json");
        heads.put(GATEWAY_SN,gateWaysn);
        get(UrlGet.urlForRegistEvent(),callback,heads);
    }


    /**
     * 查询当前报警
     * @param gateWaySn
     * @param callback
     */
    public static void queryCurrentAlarm(String gateWaySn ,GuiwuApiCallback callback){
        get(UrlGet.urlForAlarmNow(),callback,GATEWAY_SN,gateWaySn);
    }


    /**
     * 查询历史报警
     * @param gateWaySn
     * @param startTime
     * @param endTime
     * @param callback
     */
    public static void queryHistoryAlarm(String gateWaySn, Date startTime,Date endTime,GuiwuApiCallback callback){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String start = sf.format(startTime);
        String end = sf.format(endTime);
        Map<String ,String> heads = new HashMap<>();
        heads.put("startTime",start);
        heads.put("endTime",end);
        heads.put(GATEWAY_SN,gateWaySn);
        get(UrlGet.urlForAlarmHistory(),callback,heads);
    }


    /**
     * 获取网关分享者
     * @param gateWaySn
     * @param callback
     */
    public static void getGateWayShareName(String gateWaySn,GuiwuApiCallback callback){
        get(UrlGet.urlForGateWayShareName(),callback,GATEWAY_SN,gateWaySn);
    }

    /**
     * 获取网关连接类型
     * @param gateWaySn
     * @param callback
     */
    public static void getGateWayConnectType(String gateWaySn,GuiwuApiCallback callback){
        get(UrlGet.urlForGateWayConnectType(),callback,GATEWAY_SN,gateWaySn);
    }
}
